package com.shanzmoo.stm.admin.web;

import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.core.manager.store.StoreDeliveryBillManager;
import com.shanzmoo.core.manager.store.StoreMemberStockManager;
import com.shanzmoo.db.store.entity.query.StoreDeliveryBillQuery;
import com.shanzmoo.db.store.entity.query.StoreMemberStockIoQuery;
import com.shanzmoo.db.store.entity.vo.StoreDeliveryBillVO;
import com.shanzmoo.db.store.entity.vo.StoreMemberStockIoVO;
import com.shanzmoo.db.store.entity.vo.StoreMemberStockVO;
import com.shanzmoo.stm.admin.domain.query.StoreMemberStockSelfQuery;
import com.shanzmoo.stm.admin.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 库存 控制层
 *
 * @author Gzhao 2021-03-08
 */
@Slf4j
@Api(tags = "库存")
@RestController
@RequestMapping("/store/stock")
public class StoreMemberStockController {

    @Autowired
    private StoreMemberStockManager storeMemberStockManager;

    @Autowired
    private StoreDeliveryBillManager storeDeliveryBillManager;

    @ApiOperation(value = "库存分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<StoreMemberStockVO>> pageList(@Valid @RequestBody StoreMemberStockSelfQuery query) {
        PageRespBase<StoreMemberStockVO> pageList = storeMemberStockManager.queryPage(query.convertBo());
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "库存流水分页列表")
    @PostMapping("/io/pageList")
    public SHttpResponse<PageRespBase<StoreMemberStockIoVO>> pageList(@Valid @RequestBody StoreMemberStockIoQuery query) {
        PageRespBase<StoreMemberStockIoVO> pageList = storeMemberStockManager.queryIoPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "发货单分页列表")
    @PostMapping("/delivery-bill/pageList")
    public SHttpResponse<PageRespBase<StoreDeliveryBillVO>> pageList(@Valid @RequestBody StoreDeliveryBillQuery query) {
        PageRespBase<StoreDeliveryBillVO> pageList = storeDeliveryBillManager.queryPage(query);
        return SHttpResponse.ok(pageList);
    }

    @ApiOperation(value = "取消发货单")
    @GetMapping("/delivery-bill/cancel")
    public SHttpResponse<Boolean> cancel(
            @NotEmpty(message = "发货单id不允许为空") @ApiParam(value = "发货单id") @RequestParam(value = "id") Integer id,
            @NotEmpty(message = "取消原因不允许为空") @ApiParam(value = "取消原因")  @RequestParam(value = "cancelReason") String cancelReason
    ) {
        SResponse<Boolean> re = storeDeliveryBillManager.cancel(id, cancelReason);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "导入签出")
    @PostMapping("/importLogistics")
    public SHttpResponse<Boolean> importByExcel(@RequestParam("file") MultipartFile file) {
        if (file == null || file.isEmpty()) {
            log.error("文件上传失败，上传的文件为空！");
            return SHttpResponse.fail(Constant.ERRCODE_PARAM, "上传文件不能为空");
        }
        SResponse<Boolean> re = storeDeliveryBillManager.importByExcel(file, SecurityUtil.getCurrentUserId());
        return SHttpResponse.transform(re);
    }

 }
